package com.sskj.aikeeper.login;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.ValueScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.sskj.aikeeper.R;
import com.sskj.aikeeper.component.DaggerUserDataComponent;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.HeaderBean;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.EditUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.APP_LOGIN_NUMBER)
public class LoginActivity extends BaseActivity<LoginPresenter> {

    @BindView(R.id.login_return_iv)
    ImageView loginReturnIv;
    @BindView(R.id.hintTv)
    TextView hintTv;
    @BindView(R.id.login_register_tv)
    TextView loginRegisterTv;
    @BindView(R.id.login_number_et)
    EditText loginNumberEt;
    @BindView(R.id.login_password_et)
    EditText loginPasswordEt;
    @BindView(R.id.login_yincang_iv)
    ImageView loginYincangIv;
    @BindView(R.id.login_bt)
    Button loginBt;
    @Inject
    UserViewModel userViewModel;
    @BindView(R.id.login_forget_tv)
    TextView loginForgetTv;
    private AndroidNextInputs inputs;
    private boolean isPwd = true;
    private Disposable timeDis;
    private WidgetAccess access;
    private AndroidNextInputs inputsCheckCode;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_login;
    }

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        RxBus.getDefault().register(this);

        ClickUtil.click(loginReturnIv, this::onBackPressed);

        ClickUtil.click(loginRegisterTv, () -> {
            ARouter.getInstance().build(RConfig.APP_REGISTER_NUMBER).navigation();
        });

        ClickUtil.click(loginBt, () -> {
            if (isEmpty(loginNumberEt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_app_activity_register190));
                return;
            }
            if (!CheckUtil.isMobile(loginNumberEt.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_loginActivity1));
                return;
            }
            if (isEmpty(loginPasswordEt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_app_activity_change_login180));
                return;
            }
            if (!CheckUtil.isPayPwd(loginPasswordEt.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_loginActivity2));
                return;
            }
            mPresenter.login(
                    loginNumberEt.getText().toString(),
                    loginPasswordEt.getText().toString());
        });
        ClickUtil.click(loginForgetTv, () -> {
            ARouter.getInstance().build(RConfig.APP_FORGET_NUMBER).navigation();
        });
        ClickUtil.click(300, loginYincangIv, () -> {
            EditUtil.togglePs(loginPasswordEt, loginYincangIv);
        });
    }

    @Override
    public void initData() {
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    public void login(HeaderBean headerBean) {
        SPUtil.put(SPConfig.LOGIN, true);
        SPUtil.put(SPConfig.TOKEN, headerBean.getToken());
        SPUtil.put(SPConfig.ACCOUNT, headerBean.getAccount());
        HttpHeaders commonHeaders = new HttpHeaders();
        commonHeaders.put(SPConfig.ACCOUNT, headerBean.getAccount());
        commonHeaders.put(SPConfig.TOKEN, headerBean.getToken());
        commonHeaders.put("systemType", "ANDROID");
        OkGo.getInstance().addCommonHeaders(commonHeaders);
        ARouter.getInstance().build(RConfig.APP_MAIN).navigation();
        userViewModel.update();
        finish();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);

    }

}
